/*
 * Copyright 2010 The Alive System, Inc. All rights reserved.
 */

package org.alive.ee.app.repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;

/**
 * @see org.springframework.jdbc.core.RowMapperResultSetExtractor
 *
 */
public class PageResultSetExtractor<T> implements ResultSetExtractor<List<T>> {

    private final RowMapper<T> rowMapper;

    public PageResultSetExtractor(RowMapper<T> rowMapper) {
        this.rowMapper = rowMapper;
    }

    // @SuppressWarnings("unchecked")
    public List<T> extractData(ResultSet rs) throws SQLException, DataAccessException {
        // System.out.println(rs.getClass().getName());
        List<T> lstData = new ArrayList<T>();
        for (int i = 1; rs.next(); i++) {
            lstData.add(rowMapper.mapRow(rs, i));
        }
        return lstData;
    }
}
